package com.wip.reportingclient.services.impls;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.wip.reportingclient.services.domains.ListData;
import com.wip.reportingclient.services.domains.ReportData;
import com.wip.reportingclient.services.domains.ReportFormat;
import com.wip.reportingclient.services.dtos.RequestDto;
import com.wip.reportingclient.services.interfaces.IXlsService;
import com.wip.reportingclient.services.interfaces.IPdfService;
import com.wip.reportingclient.services.interfaces.IWebService;
import com.wip.reportingclient.services.interfaces.IXlsxService;
import com.wip.reportingclient.ui.components.FormData;

@Service
public class WebServiceImpl implements IWebService {

	private String url = "http://localhost:8080/reporting-service/Reporting/data";
	private String allClientsUrl = "http://localhost:8080/reporting-service/Reporting/allclients";
	private String allPagesUrl = "http://localhost:8080/reporting-service/Reporting/allpages";
	private String allLogsUrl = "http://localhost:8080/reporting-service/Reporting/alllogs";

	@Autowired
	private RestTemplate restTemplate;

	// @Autowired
	// private IDownloadService downloadService;

	@Autowired
	private IXlsService xlsService;

	@Autowired
	private IXlsxService xlsxService;

	@Autowired
	private IPdfService pdfService;

	/*
	 * @Autowired private List<MarshallingHttpMessageConverter>
	 * marshallingConverter;
	 */

	@Override
	public void generateReport(HttpServletRequest request,
			HttpServletResponse response, ReportFormat reportType,
			FormData formData) {
		// A simple GET request, the response will be mapped to Example.class

		RequestDto obj = new RequestDto();
		obj.setClients(formData.getClients());
		obj.setLogs(formData.getLogs());
		obj.setPages(formData.getPages());
		// obj.setStuff("stuff");

		// set your headers
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		// set your entity to send
		HttpEntity<RequestDto> entity = new HttpEntity<RequestDto>(obj, headers);

		// pass object in request
		ResponseEntity<ReportData> result = restTemplate.exchange(url,
				HttpMethod.POST, entity, ReportData.class);

		// StringBuilder sb = new StringBuilder(result.getHeader());
		// result.getBody()
		result.getBody().getDataMap();

		switch (reportType) {
		case PDF:
			System.out.println("pdf");
			pdfService.createReport(response, formData, result.getBody());
			break;
		case XLS:
			System.out.println("xls");
			xlsService.createReport(response, formData, result.getBody());
			break;
		case XLSX:
			System.out.println("xlsx");
			xlsxService.createReport(response, formData, result.getBody());
			break;
		case CSV:
			System.out.println("csv");
			break;

		default:
			System.out.println("pdf");
			break;
		}

		// Delegate to downloadService. Make sure to pass an instance of
		// HttpServletResponse

	}

	public ListData fetchAllClients() {
		return restTemplate.getForObject(allClientsUrl, ListData.class);
	}

	@Override
	public ListData fetchAllLogs() {
		return restTemplate.getForObject(allLogsUrl, ListData.class);
	}

	@Override
	public ListData fetchAllPages() {
		return restTemplate.getForObject(allPagesUrl, ListData.class);
	}

}
